package com.jichaoyun.controller;


import com.jichaoyun.common.model.PagedResult;
import com.jichaoyun.common.model.Result;
import com.jichaoyun.model.entity.StationInfo;

import com.jichaoyun.service.IRealdataCacheService;
import com.jichaoyun.service.IRealdataService;
import com.jichaoyun.service.IStationInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhen
 */
@RestController
@Api(tags = {"管损接口"})
@RequestMapping("/sys/PipeLossInfo")
public class PipeLossController {
    @Autowired
    private IStationInfoService stationInfoService;

    @Autowired
    private IRealdataService realdataService;

    @Autowired
    private IRealdataCacheService realdataCacheService;


    @ApiOperation("管损报表查询-查询所有的非用户站点")
    @GetMapping("/getAllLineStationName")
    public Result<List<Map<String,Object>>> getAllLineStationName(){
        List<Map<String,Object>> res = new ArrayList<>();
        List<StationInfo> allLineStationName = stationInfoService.getAllLineStation();
        for (StationInfo stationInfo : allLineStationName) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("StationName",stationInfo.getStationName());
            map.put("StationNo",stationInfo.getStationNo());
            res.add(map);
        }
        return  Result.success(res);
    }

    @ApiOperation(value = "管损报表查询-查询左边的list,某个线路，总路线传-1就好了",notes = "日期和时间是是yyyy-MM-dd HH:mm")
    @GetMapping("/getLineLossForm")
    public Result<PagedResult> getLineLossForm(String startTime,String endTime, Integer pageNo, Integer LineId){

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime start = LocalDateTime.parse(startTime,dateTimeFormatter);
        LocalDateTime end = LocalDateTime.parse(endTime,dateTimeFormatter);

        PagedResult pagedResult = realdataService.getLineLossForm(pageNo,start,end,LineId);

        return Result.success(pagedResult);
    }
    @ApiOperation(value = "管损自定义查询-查询左边的list,某个线路，总路线传-1就好了",notes = "日期和时间是是yyyy-MM-dd HH:mm")
    @GetMapping("/getLineLossForm1")
    public Result<PagedResult> getLineLossForm1(String startTime,String endTime, Integer pageNo, Integer LineId){

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime start = LocalDateTime.parse(startTime,dateTimeFormatter);
        LocalDateTime end = LocalDateTime.parse(endTime,dateTimeFormatter);

        PagedResult pagedResult = realdataService.getLineLossForm1(pageNo,start,end,LineId);

        return Result.success(pagedResult);
    }

    @ApiOperation(value = "管损报表查询-查询右边的图，默认查找最近十分钟的数据")
    @GetMapping("/getTenMinutesLineLossForm")
    public Result<Map<String,Object>> getTenMinutesLineLossForm(Integer StationNo){

        Map<String,Object> res = realdataCacheService.getTenMinutesLineLossForm(StationNo);

        return Result.success(res);
    }

    @ApiOperation(value = "管损报表查询-查询右边的图，选择区间内的数据",notes = "日期和时间是是yyyy-MM-dd HH:mm，精确到分钟")
    @GetMapping("/getPeriodLineLossForm")
    public Result<Map<String,Object>> getPeriodLineLossForm(Integer LineId,String startTime,String endTime){

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime start = LocalDateTime.parse(startTime,dateTimeFormatter);
        LocalDateTime end = LocalDateTime.parse(endTime,dateTimeFormatter);

        Map<String,Object> res = realdataService.getPeriodLineLossForm(LineId,start,end);

        return Result.success(res);
    }

    @ApiOperation(value = "(App)管损报表查询-查询图",notes = "日期和时间是是yyyy-MM-dd HH:mm，精确到分钟")
    @GetMapping("/getAppPeriodLineLossForm")
    public Result<Map<String,Object>> getAppPeriodLineLossForm(Integer lineId,String startTime,String endTime){

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        LocalDateTime start = LocalDateTime.parse(startTime,dateTimeFormatter);
        LocalDateTime end = LocalDateTime.parse(endTime,dateTimeFormatter);

        Map<String,Object> res = realdataService.getAppPeriodLineLossForm(lineId,start,end);

        return Result.success(res);
    }


}
